﻿using System;
using System.Collections.Generic;
using System.Text;
using He.Common;
using LitJson;

namespace He.API.WeiXin
{
    public class GroupHandle
    {
        /// <summary>
        /// 获取分组列表
        /// </summary>
        /// <returns></returns>
        public static Dictionary<int, string> GetGroups()
        {
            string access_token = CheckSignature.GetAccessToken();
            string result = HttpHelper.HttpGet("https://api.weixin.qq.com/cgi-bin/groups/get?access_token=" + access_token);
            if (result.Contains("errcode"))
            {
                return null;
            }
            else
            {
                List<Groups.Group> lst = new List<Groups.Group>();
                LitJson.JsonData jd = LitJson.JsonMapper.ToObject<LitJson.JsonData>(result);
                string json = jd[0].ToJson();
                lst = LitJson.JsonMapper.ToObject<List<Groups.Group>>(json);
                Dictionary<int, string> dic = new Dictionary<int, string>();
                foreach (Groups.Group group in lst)
                {
                    dic.Add(group.id, group.name);
                }
                return dic;
            }
        }
    }
}
